﻿#include "LoginSystem.h"
#include <QtWidgets/QApplication>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <AdminSystem.h>
#include <UserSystem.h>
#include <Global_Class.h>
//TODO 在这个位置需要读入所有文件

#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <Constants.h>
#include <AllData.h>
using namespace Constants;


LoginSystem::LoginSystem(QWidget* parent)//不要在这里初始化ui对象
    : QMainWindow(parent)
{
  
}

void LoginSystem::initLoginSystem()  //在这里初始化
{
    // 在此处初始化UI组件
    ui.setupUi(this);
    SetupLoginOperations();
}

LoginSystem::~LoginSystem()
{

}

void LoginSystem::SetupLoginOperations()
{
	allData = new AllData(); // 初始化全局数据对象 
    try {
        allData->User_ReadFromFile(USER_INFORMATION_FILE_POSITION); // 读取用户数据
		allData->Train_Schedeles_ReadFromFile(TRAIN_SCHEDULE_FILE_POSITION); // 读取火车时刻表数据
		allData->Station_ReadFromFile(STATION_INFORMATION_FILE_POSITION); // 读取车站数据
		allData->Seat_ReadFromFile(SEAT_INFORMATION_FILE_POSITION); // 读取座位数据
		allData->Order_ReadFromFile(ORDER_INFORMATION_FILE_POSITION); // 读取订单数据
		allData->Train_ReadFromFile(TRAIN_INFORMATION_FILE_POSITION); // 读取列车数据
        allData->SeatOccupy_ReadFromFile("seat_occupy.txt");
    }
    catch (const std::runtime_error& e) {
        QMessageBox::critical(this, "Error", QString::fromStdString(e.what()));
        return;
	}
	QPushButton* loginButton = ui.EnterBtn;
	QPushButton* cancelButton = ui.CancelBtn;
	QPushButton* registerButton = ui.RegisterBtn; // 假设UI有RegisterBtn

    connect(loginButton, &QPushButton::clicked, this, &LoginSystem::onEnterBtn);
    connect(cancelButton, &QPushButton::clicked, this, [this]() {
        QMessageBox::information(this, "Cancel", "Login cancelled.");
        });
    connect(registerButton, &QPushButton::clicked, this, &LoginSystem::onRegisterBtn);
	// 绑定回车键于登录按钮，无论焦点在哪个输入框，实现按回车键登录
    connect(ui.UserLine, &QLineEdit::returnPressed, this, &LoginSystem::onEnterBtn);
    connect(ui.PasswordLine, &QLineEdit::returnPressed, this, &LoginSystem::onEnterBtn);
}

void LoginSystem::onEnterBtn() {
    QString username = ui.UserLine->text();
    QString password = ui.PasswordLine->text();
    //TODO 实现用户名与密码校验
    
  //  if (username.toStdString() == "admin" && password.toStdString() == "123456"){ //管理员测试
		//QMessageBox::information(this, "Login Success", "Welcome, " + username + "!");
		//AdminSystem* adminWindow = new AdminSystem();
		//adminWindow->show();
		//this->close(); // 关闭登录窗口
  //      return;
  //  }
    /*
    else if (username.toStdString() == "user" && password.toStdString() == "123456") { //用户测试
		QMessageBox::information(this, "Login Success", "Welcome, " + username + "!");
		UserSystem* userWindow = new UserSystem();
		userWindow->show();
        this->close(); // 关闭登录窗口
		return;
    }
    */

	// 遍历用户队列，检查用户名和密码 用户信息需要保持到其他窗口直到退出登录
    for (auto user : allData->usersQueue) {
        if (user.getUsername() == username.toStdString() && user.getPassword() == password.toStdString()) {
            if(user.getState() == "0") { // 如果用户状态为0，表示未激活
                QMessageBox::warning(this, "Login Failed", "您的账户已被管理员封禁，请联系管理员解封");
                return;
			}
            else if (user.getRole() == "admin") { // 如果是管理员
                //UI界面优化前
                //QMessageBox::information(this, "Login Success", "Welcome, " + username + "!");
                
                
                //优化后
                // 创建自定义样式的消息框
                QMessageBox msgBox;
                msgBox.setWindowTitle("登录成功");
                msgBox.setText("欢迎, " + username + "!");
                msgBox.setIcon(QMessageBox::Information);

                // 设置样式表
                msgBox.setStyleSheet(R"(
                    QMessageBox {
                    background-color: #f8f9fa;
                    border: 1px solid #dee2e6;
                    border-radius: 8px;
                    font-family: "SimSun", "宋体";
                }
                QMessageBox QLabel {
                    color: #212529;
                    font-size: 14px;
                    font-family: "SimSun", "宋体";
                }
                QMessageBox QPushButton {
                    background-color: #007bff;
                    color: white;
                    border: none;
                    border-radius: 4px;
                    padding: 8px 16px;
                    font-family: "SimSun", "宋体";
                    font-size: 13px;
                    min-width: 80px;
                }
                QMessageBox QPushButton:hover {
                    background-color: #0056b3;
                }
                QMessageBox QPushButton:pressed {
                    background-color: #004085;
                }
            )");
                msgBox.exec();

				adminWindow = new AdminSystem(); // 创建管理员系统窗口
                adminWindow->show();
            }
            else { // 如果是普通用户
                QMessageBox::information(this, "Login Success", "Welcome, " + username + "!");
				userWindow = new UserSystem(); // 创建用户系统窗口
                userWindow->setCurrentUser(user); // 修复错误：设置当前用户信息  
                userWindow->show();
            }
            this->close(); // 关闭登录窗口
            return;
        }
	}
    QMessageBox::warning(this, "Login Failed", "Invalid username or password.");
}

void LoginSystem::onRegisterBtn() {
    RegisterDialog dlg(this);
    dlg.exec();
}



